﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoNideBlending : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //if (btnSave.CommandArgument == "")
        //{
        //    tbl_siconideblending model = new tbl_siconideblending();
        //    model.Blending_Date = txtBlending_Date.Text.Trim().toDateTimeWithoutError();
        //    model.Seed_Drum = txtSeed_Drum.Text.Trim();
        //    model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
        //    model.Notes = txtNotes.Text.Trim();
        //    model.CREATED_BY = User.Identity.Name;
        //    model.CREATED_DATE = DateTime.Now;



        //    BLL_tbl_siconideblending bll = new BLL_tbl_siconideblending();
        //    bll.insert(model);
        //}
        //else
        //{
        //    tbl_siconideblending model = new tbl_siconideblending();
        //    model.ID = Convert.ToInt32(btnSave.CommandArgument);
        //    model.Blending_Date = txtBlending_Date.Text.Trim().toDateTimeWithoutError();
        //    model.Seed_Drum = txtSeed_Drum.Text.Trim();
        //    model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
        //    model.Notes = txtNotes.Text.Trim();
        //    model.UPDATED_BY = User.Identity.Name;
        //    model.UPDATED_DATE = DateTime.Now;



        //    BLL_tbl_siconideblending bll = new BLL_tbl_siconideblending();
        //    bll.update(model);
        //    btnSave.CommandArgument = "";
        //    Response.Redirect(Request.Url.AbsolutePath);
        //}
    }

    public List<tbl_siconideblending_drums> getSiconideblending_drums()
    {
        List<tbl_siconideblending_drums> list = new List<tbl_siconideblending_drums>();

        return list;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_siconideblending model = new tbl_siconideblending();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_siconideblending bll = new BLL_tbl_siconideblending();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                ViewState["ID"]=id;
                BLL_tbl_siconideblending bll = new BLL_tbl_siconideblending();
                List<tbl_siconideblending> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_siconideblending tbl in list)
                {
                    //btnSave.CommandArgument = tbl.ID.ToString();
                    txtBlending_Date.Text = tbl.Blending_Date.ToString("yyyy-MM-dd");
                    txtSeed_Drum.Text = tbl.Seed_Drum;
                    txtCharge_Number.Text = tbl.Charge_Number.ToString();
                    txtNotes.Text = tbl.Notes;
                    

                }
                addInputDrums();
            }
        }
    }



    protected void ddlBag_Drum_Input_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlBag_Drum_Input.Items.Insert(0, li);
    }


    private void addInputDrums()
    {
        BLL_tbl_siconideblending bll = new BLL_tbl_siconideblending();
        int countvessels = bll.countsiconideblending_vessels(Convert.ToInt32(ViewState["ID"]));
        DataTable dt = bll.getDataSiconideblending_vessels(Convert.ToInt32(ViewState["ID"]));
        string script = "";
        for (int i = 1; i <= countvessels; i++)
        {
            script += "$($($('#mytable1 tbody>tr:last>')).find('select')).val('" + dt.Rows[i - 1]["Drum_Bag"] + "');  $($($('#mytable1 tbody>tr:last>')).find('input')).val('" + dt.Rows[i - 1]["Drum_Bag_Number"] + "');";

            if (i < countvessels)
                script += "addRow('mytable1');";
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), "AddingDrum", script, true);
    }
}